package nebenlaeufig;

import java.util.ArrayList;
import java.util.Map;

public class NFirstPartition {

	ArrayList<Integer> nodes;
	Map<Integer,Integer> values;
	ArrayList<NMesh> meshes;
	boolean ausgabe;
	
	/**
	 * the constructor, which gets called to calculate the first partition of a graph g
	 * @param graph g
	 * @param ausgabe output on/off
	 */
	public NFirstPartition(NGraph graph, boolean ausgabe){
		this.ausgabe=ausgabe;
		nodes = graph.getNodes();
		values = graph.getValues();
		
		meshes = new ArrayList<NMesh>();
		
		calcFirstPartition();
	}
	
	/**
	 * groups the nodes of the graph in the first-partition-meshes and saves them locally
	 */
	private void calcFirstPartition(){
		//iterate over all nodes
		for(int i=0;i<nodes.size();i++){
			//actual node and its value
			int node = nodes.get(i),
				value = values.get(node);
			NMesh actualMesh = null;
			boolean found = false;
			//search if there is already a mesh with the given value
			for(int j=0;j<meshes.size();j++){
				actualMesh = meshes.get(j);
				if(values.get(actualMesh.get(0))==value){
					actualMesh.add(node);
					found = true;
					break;
				}
			}
			//if there was no fitting mesh, add a new one
			if(!found){
				actualMesh = new NMesh();
				actualMesh.add(node);
				meshes.add(actualMesh);
			}
		}
		
				if(ausgabe){
					System.out.print("FirstPartition: ");
					for(int i=0;i<meshes.size();i++)
						System.out.print("(" + meshes.get(i)+")");
					System.out.print("\n");
				}
	}
	
	/**
	 * to get the meshes after first partition
	 * @return the meshes
	 */
	public ArrayList<NMesh> getMeshes(){
		return meshes;
	}
}
